package com.authenticateCenter.controller;

import com.authenticateCenter.assembler.RoleAssembler;
import com.authenticateCenter.domain.dto.RoleDto;
import com.authenticateCenter.domain.entity.AuthorityModule;
import com.authenticateCenter.domain.entity.Role;
import com.authenticateCenter.domain.vo.RoleVo;
import com.authenticateCenter.service.IRoleAuthorityService;
import com.authenticateCenter.service.IRoleService;
import com.authenticateCenter.service.IUserRoleService;
import com.authenticateCenter.util.StringUtil;
import com.common.mybaits.CustomPage;
import com.common.result.ResponseResultAdvice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import static com.authenticateCenter.factory.RoleFactory.ROLE_CONVERT;

/**
 * @author 大忽悠
 * @create 2022/9/15 11:09
 */
@Validated
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
@ResponseResultAdvice
@Api(tags = "角色管理")
public class RoleController {
    private final IRoleService iRoleService;
    private final IUserRoleService iUserRoleService;
    private final IRoleAuthorityService iRoleAuthorityService;
    private final RoleAssembler roleAssembler;

    @ApiOperation("添加角色")
    @PostMapping("/add")
    public Integer addRole(@RequestBody@Valid RoleDto roleDto){
        Role role = ROLE_CONVERT.toRole(roleDto);
        role.insertCheck(iRoleService);
        iRoleService.save(role);
        return role.getId();
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/delete")
    public void deleteRole(@RequestParam("id")Integer roleId){
        iRoleService.removeRole(roleId,iRoleAuthorityService);
    }

    @ApiOperation("禁用角色")
    @PostMapping("/disable")
    public void disableRole(@RequestParam("id")Integer roleId) {
        iRoleService.updateById(Role.builder().id(roleId).enabled(0).build());
    }

    @ApiOperation("开启角色")
    @PostMapping("/enable")
    public void enableRole(@RequestParam("id")Integer roleId) {
        iRoleService.updateById(Role.builder().id(roleId).enabled(1).build());
    }

    @ApiOperation("返回已经添加的所有角色")
    @GetMapping("/list")
    public List<RoleVo> getRoleList(CustomPage customPage){
        return roleAssembler.assemblers(customPage,null);
    }

    @ApiOperation("返回某个用户具有的角色集合")
    @GetMapping("/listOfUser")
    public List<RoleVo> getRoleListOfUser(@RequestParam("userId")Integer userId, CustomPage customPage){
        return roleAssembler.assemblers(customPage,userId);
    }

    @ApiOperation("发放角色给某个用户")
    @PostMapping("/releaseByIdList")
    public void releaseRoleToUserByIdList(@RequestParam("uid")Integer uid,@ApiParam("roleId按照英文逗号分隔") @RequestParam("roleIdList")String roleIdList){
        iUserRoleService.releaseAuthorityToRoleByIdList(uid, StringUtil.strIdToListInt(roleIdList));
    }

    @ApiOperation("发放角色给某个用户")
    @PostMapping("/releaseByNameList")
    public void releaseRoleToUserByNameList(@RequestParam("uid")Integer uid,@ApiParam("roleName按照英文逗号分隔") @RequestParam("roleNameList")String roleNameList){
        iUserRoleService.releaseAuthorityToRoleByNameList(uid, StringUtil.strNameToListName(roleNameList));
    }

    @ApiOperation("收回某个用户的相关角色")
    @PostMapping("/revoke")
    public void revokeUserOfRole(@RequestParam("uid")Integer uid,@ApiParam("roleId按照英文逗号分隔")@RequestParam("roleIdList")String roleIdList){
        iUserRoleService.revokeRoleOfAuthority(uid,StringUtil.strIdToListInt(roleIdList));
    }
}
